package com.xyj.rxhui.week00;

import com.xyj.common.ListNode;

/**
 * 倒数第K个节点
 * @author GuXiaoYao
 * @version 1.0.0
 * @since 2021/11/26
 */
public class T4 {





    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode help=new ListNode(-1);
        help=head;
        ListNode x = findTheLastN(help, n+1);
        x.next=x.next.next;
        return help.next;
    }


    public ListNode findTheLastN(ListNode head,int lastN){

        ListNode fast=head;
        // fast先走n步
        for (int i = 0; i < lastN; i++) {
            fast=fast.next;
        }
        ListNode slow=head;
        while (fast!=null) {
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
}
